package cn.wubo.file.preview.demo;

import cn.wubo.file.preview.core.FilePreviewInfo;
import cn.wubo.file.preview.record.IFilePreviewRecord;
import cn.wubo.sql.util.*;
import com.alibaba.druid.DbType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Component
public class H2FilePriviewRecordImpl implements IFilePreviewRecord {

    private static final String HISTORY = "file_preview_history";

    private static ConnectionPool connectionPool = new ConnectionPool(new ConnectionParam());

    @Override
    public FilePreviewInfo save(FilePreviewInfo filePreviewInfo) {
        if (!StringUtils.hasLength(filePreviewInfo.getId())) {
            filePreviewInfo.setId(UUID.randomUUID().toString());
            connectionPool.run((connection, sql) -> {
                return ExecuteSqlUtils.executeUpdate(connection, sql);
            }, ModelSqlUtils.insertSql(HISTORY, filePreviewInfo));
        } else {
            connectionPool.run((connection, sql) -> {
                return ExecuteSqlUtils.executeUpdate(connection, sql);
            }, ModelSqlUtils.updateByIdSql(HISTORY, filePreviewInfo));
        }
        return filePreviewInfo;
    }

    @Override
    public List<FilePreviewInfo> list(FilePreviewInfo filePreviewInfo) {
        SQL<FilePreviewInfo> sql = ModelSqlUtils.selectSql(HISTORY, new FilePreviewInfo());
        if (StringUtils.hasLength(filePreviewInfo.getId())) sql.addWhereEQ("id", filePreviewInfo.getId());
        if (StringUtils.hasLength(filePreviewInfo.getFileName()))
            sql.addWhereLIKE("fileName", filePreviewInfo.getFileName());
        if (StringUtils.hasLength(filePreviewInfo.getOriginalFilename()))
            sql.addWhereLIKE("originalFilename", filePreviewInfo.getOriginalFilename());
        if (StringUtils.hasLength(filePreviewInfo.getFilePath()))
            sql.addWhereLIKE("filePath", filePreviewInfo.getFilePath());
        sql.parse();
        return connectionPool.run((connection, fileInfoSQL) -> {
            return ExecuteSqlUtils.executeQuery(connection, fileInfoSQL);
        }, sql);
    }

    @Override
    public FilePreviewInfo findById(String s) {
        FilePreviewInfo query = new FilePreviewInfo();
        query.setId(s);
        List<FilePreviewInfo> list = list(query);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public Boolean delete(FilePreviewInfo filePreviewInfo) {
        SQL<?> sql = SQL.delete().table(HISTORY);
        if (StringUtils.hasLength(filePreviewInfo.getId())) sql.addWhereEQ("id", filePreviewInfo.getId());
        if (StringUtils.hasLength(filePreviewInfo.getFileName()))
            sql.addWhereLIKE("fileName", filePreviewInfo.getFileName());
        if (StringUtils.hasLength(filePreviewInfo.getOriginalFilename()))
            sql.addWhereLIKE("originalFilename", filePreviewInfo.getOriginalFilename());
        if (StringUtils.hasLength(filePreviewInfo.getFilePath()))
            sql.addWhereLIKE("filePath", filePreviewInfo.getFilePath());
        sql.parse();
        connectionPool.run((connection, fileInfoSQL) -> {
            return ExecuteSqlUtils.executeUpdate(connection, fileInfoSQL);
        }, sql);
        return Boolean.TRUE;
    }

    @Override
    public void init() {
        if (Boolean.FALSE.equals(connectionPool.run((connection, sql) -> {
            return ExecuteSqlUtils.isTableExists(connection, sql);
        }, SQL.tableExists(HISTORY, DbType.h2)))) {
            connectionPool.run((connection, sqlStr) -> {
                return ExecuteSqlUtils.executeUpdate(connection, sqlStr);
            }, ModelSqlUtils.createSql(HISTORY, FilePreviewInfo.class));
        }
    }
}
